<?php
/**
 * @author Pham Van Diep
 * @copyright 2014
 * @description: save personal
 */
// not login
// not perm add
if(!isset($AppUI->id) ||
    !isset($_SESSION['personal'])||
	(isset($_SESSION['personal']) && ($_SESSION['personal']&1<<2) == 0)||
	! isset($_POST['personalid'])

) {
    redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}
// data input

$arrErr = array();
$intPersonalId = intval($_POST['personalid']);
// get to backup
$oldRecord = $this->personal->get($AppUI->id,$intPersonalId);
$strFirstName = CI_Function::stripTag($this->input->post('first',null));

$intLen = strlen($strFirstName);
if($intLen < VL_NAME_MINLENGTH || $intLen > VL_NAME_MAXLENGTH ) {
    array_push($arrErr,$arrLocale['firstname_range']);
}
$strLastName = CI_Function::stripTag($this->input->post('last',null));
$intLen = strlen($strLastName);
if($intLen < VL_NAME_MINLENGTH || $intLen > VL_NAME_MAXLENGTH) {
    array_push($arrErr,$arrLocale['lastname_range']);
}
$strNickName = CI_Function::stripTag($this->input->post('nickname',null));
$intLen = strlen($strNickName);
if($intLen < VL_NICKNAME_MINLENGTH || $intLen > VL_NICKNAME_MAXLENGTH) {
    array_push($arrErr,$arrLocale['nickname_range']);
}
$strBirth = CI_Function::stripTag($this->input->post('birth',null));
if(!CI_Validate::isDate($strBirth)) {
    array_push($arrErr,$arrLocale['date_invalid']);
}
$strPlace = CI_Function::stripTag($this->input->post('place',null));
if(empty($strPlace)) {
    array_push($arrErr,$arrLocale['place_required']);
}

$strLink = CI_Function::stripTag($this->input->post('link',null));
if(!empty($strLink) ){
	$this->load->library('upload');
    $arrInfo = $this->upload->do_upload_url($strLink);
	if(count($arrInfo) > 0)  $strAvatar = CI_Function::getUrl($arrInfo['path'].$arrInfo['realname'].'.'.$arrInfo['extension']);  
}else if(isset($_FILES['logo']['name'])) {
	 $this->load->library('upload');
     $arrInfo = $this->upload->do_upload('logo'); 
	 if(count($arrInfo) > 0)  $strAvatar = CI_Function::getUrl($arrInfo['path'].$arrInfo['realname'].'.'.$arrInfo['extension']);  
}
if(!isset($strAvatar))	$strAvatar = $oldRecord->avatar;	

$strShort    	    = CI_Function::stripTag($this->input->post('sshort',null));
$intLen = strlen($strShort);
if ($intLen < VL_SHORT_MINLENGTH || $intLen > VL_SHORT_MAXLENGTH ) {
    array_push($arrErr, $arrLocale['short_range']);
}			
$strDes             = CI_Function::stripTag($this->input->post('description',null));
$intLen = strlen($strDes);
if ($intLen < VL_CONTENT_MINLENGTH ) {
    array_push($arrErr, $arrLocale['description_minlength']);
}
$strTag             = CI_Function::stripTag($this->input->post('tag', null));
if(strlen($strTag) > VL_TAG_MAXLENGTH) {
    array_push($arrErr, $arrLocale['tag_maxlength']);
}
$type = 0;
$arrType = $this->input->post('type');
foreach ($arrType as $bit) {
	$type = $type + ( $bit +1 );	
}
if($type == 0) {
    array_push($arrErr, $arrLocale['type_required']);
}
$strFeatures = isset($_POST['feature']) ? trim($_POST['feature']):null;
if(count($arrErr) <= 0) {//allow save     
	if(isset($_SESSION['PERSONAL_ERROR'])) {unset($_SESSION['PERSONAL_ERROR']);}
	if(isset($_SESSION['PERSONAL_INPUT'])) {unset($_SESSION['PERSONAL_INPUT']);}     
    	
    $intError = $this->personal->update($AppUI->id,
									$intPersonalId,
									$strFirstName,
									$strLastName,
									$strNickName,
									CI_Function::convertStrToDate($strBirth),
									$strPlace,
									$strAvatar,
									$strTag,
									$strShort,
									$strDes,
									$type,
									$strFeatures); 
    if($intError == 0){ CI_Function::setMsg($arrLocale['success']);}
    elseif($intError == -1) { array_push($arrErr,$arrLocale['sys_add_err']);}
	else {array_push($arrErr,$arrLocale['db_err']); }
}
if ( count($arrErr) > 0 ) {       
    $_SESSION['PERSONAL_ERROR'] = $arrErr;   
	$_SESSION['PERSONAL_INPUT'] = $this->input->post(); 
    redirect(HOST_BACKEND . 'personal/edit?personalid='.$intPersonalId);
}
redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);

